/*
 * @lc app=leetcode.cn id=56 lang=javascript
 *
 * [56] 合并区间
 */

// @lc code=start
/**
 * @param {number[][]} intervals
 * @return {number[][]}
 */
var merge = function (intervals) {
    intervals.sort((a, b) => { return a[0] - b[0] })
    let res = []
    let prev = intervals[0]
    for (let i = 1; i < intervals.length; i++) {
        const curr = intervals[i]
        if (prev[1] < curr[0]) {
            res.push(prev)
            prev = curr
        } else {
            prev[1] = Math.max(curr[1],prev[1])
        }

    }
    res.push(prev)
    return res

};
// @lc code=end

